package keeto.com.vn.classified.task;

import java.io.IOException;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.database.BuiltinServiceManager;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.GetProfileInfoRequest;
import keeto.com.vn.classified.response.GetProfileInfoResponse;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

public class TaskGetProfileInfo extends AbstractTask {
	String authenKey;
	private boolean inLogonChain = false;
	public TaskGetProfileInfo(ClassifiedListener listener, ClassifiedCore core,  String authenKey) {
		super(listener, core);
		// TODO Auto-generated constructor stub
		this.authenKey =  authenKey;
	}

	@Override
	public void run() throws ClientProtocolException, JSONException,
			IOException {
		// TODO Auto-generated method stub
		GetProfileInfoRequest request = new GetProfileInfoRequest();
		request.setAuthKey(authKey);
		GetProfileInfoResponse response = json.getProfileInfo(request);
		
		if(response.getReturnCode() == IProtocol.STATUS_CODE_OK_200){
			
			BuiltinServiceManager.getInstance(core.getContext()).saveAccountInfo(response.getInfo());
			
			
		}
		
		
		for (ClassifiedListener listener : listeners) {
			listener.finishGetProfileInfo(response.getReturnCode(), response.getInfo(),response.getLink());
		}
		
	}

	@Override
	public void timeoutCallback() {
		for (ClassifiedListener listener : core.copyAndReconstructListeners(null)) {
			listener.finishGetProfileInfo(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502,null,"");
		}
		
	}

}
